﻿using NewBee.Core.Data.SQLBuilder;
using NewBee.Core.DataService;
using NewBee.Core.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NewBee.Core.Test
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            NewBee.Core.Data.DbHelper _DbHelper = new Data.DbHelper(DataConnection.ConnectionString, DataConnection.QueryConnectionString, DataConnection.DriverType);

            Console.WriteLine("NewBee.Core.RC2.Data测试启动成功!" + " " + System.DateTime.Now.ToString());

            //NewBee.Core.RC2.Data 使用说明
            #region 增删查改-使用说明

            ////---Insert

            ////1.使用sql语句增加数据

            //int _int = _DbHelper.Sql("Insert into Test( ID, Name, InTime) values( @ID, @Name, @InTime )")
            //       .Parameter(new { ID = Guid.NewGuid(), Name = "10086", InTime = System.DateTime.Now })
            //       .Execute();
            //Console.WriteLine("1.1使用sql语句增加数据:" + _int.ToString() + " " + System.DateTime.Now.ToString());

            //_int = _DbHelper.Sql("Insert into Test(ID,Name,InTime) values(@0,@1,@2)")
            //       .Parameters(Guid.NewGuid(), "10086", System.DateTime.Now)
            //       .Execute();
            //Console.WriteLine("1.2使用sql语句增加数据:" + _int.ToString() + " " + System.DateTime.Now.ToString());

            ////2.使用hashTable增加数据

            //Hashtable _Hashtable = new Hashtable();
            //_Hashtable.Add("ID", Guid.NewGuid());
            //_Hashtable.Add("Name", "10086");
            //_Hashtable.Add("InTime", DateTime.Now);
            //_int = _DbHelper.Insert("Test")
            //    .Column(_Hashtable)
            //    .Execute();

            //Console.WriteLine("2.1使用hashTable增加数据:" + _int.ToString() + " " + System.DateTime.Now.ToString());

            ////3.使用列名增加数据

            //_int = _DbHelper.Insert("Test")
            //    .Column("ID", Guid.NewGuid())
            //    .Column("Name", "10086")
            //    .Column("InTime", System.DateTime.Now)
            //    .Execute();

            //Console.WriteLine("3.1使用列名增加数据:" + _int.ToString() + " " + System.DateTime.Now.ToString());


            ////4.使用实体增加数据

            //Models.Test model = new Models.Test();
            //model.ID = Guid.NewGuid();
            //model.Name = "10086";
            //model.InTime = DateTime.Now;

            //_int = _DbHelper.Insert<Models.Test>(model, "IntID").Execute();

            //Console.WriteLine("4.1使用实体增加数据:" + _int.ToString() + " " + System.DateTime.Now.ToString());

            ////---Update

            ////1.使用sql语句进行更新
            //_int = _DbHelper.Sql("update Test set Name=@Name where IntID=@IntID")
            //    .Parameter(new { Name = "newName", IntID = 1 })
            //    .Execute();
            //Console.WriteLine("1.1使用sql语句进行更新数据:" + _int.ToString() + " " + System.DateTime.Now.ToString());

            //_int = _DbHelper.Sql("update Test set Name=@0 where IntID=@1")
            //   .Parameters("newName", 1)
            //   .Execute();
            //Console.WriteLine("1.2使用sql语句进行更新数据:" + _int.ToString() + " " + System.DateTime.Now.ToString());

            ////2.使用hashTable进行更新
            //Hashtable _Hashtable1 = new Hashtable();
            //_Hashtable1.Add("Name", "10086");
            //_Hashtable1.Add("InTime", DateTime.Now);
            //_int = _DbHelper.Update("Test")
            //    .Column(_Hashtable1)
            //    .Where("IntID =@0")
            //    .Parameters(1)
            //    .Execute();
            //Console.WriteLine("2.1使用hashTable进行更新数据:" + _int.ToString() + " " + System.DateTime.Now.ToString());

            ////3.使用列名/值对进行更新
            //_int = _DbHelper.Update("Test")
            //     .Column("Name", "10086")
            //     .Column("InTime", DateTime.Now)
            //     .Where("IntID =@0")
            //     .Parameters(1)
            //     .Execute();
            //Console.WriteLine("3.1使用Entity实体进行更新数据:" + _int.ToString() + " " + System.DateTime.Now.ToString());

            ////4.使用Entity实体进行更新
            //Models.Test model1 = new Models.Test();
            //model1.Name = "10086";
            //model1.InTime = DateTime.Now;
            //_int = _DbHelper.Update<Models.Test>("Test", model1)
            //    .PrimaryKeys("ID", "IntID")
            //    .Where("IntID =@0")
            //    .Parameters(1)
            //    .Execute();
            //Console.WriteLine("4.1使用Entity实体进行更新数据:" + _int.ToString() + " " + System.DateTime.Now.ToString());


            ////---Delete

            ////1.使用sql语句进行删除
            //_int = _DbHelper.Sql("delete from Test1 where Name =@Name")
            //   .Parameter(new { Name = "SQLINSERT" })
            //   .Execute();
            //Console.WriteLine("1.1使用sql语句进行删除数据:" + _int.ToString() + " " + System.DateTime.Now.ToString());

            //_int = _DbHelper.Sql("delete from Test1 where Name=@0")
            //   .Parameters("SQLINSERT")
            //   .Execute();
            //Console.WriteLine("1.2使用sql语句进行删除数据:" + _int.ToString() + " " + System.DateTime.Now.ToString());

            ////2.使用wehre条件进行删除操作
            //_int = _DbHelper.Delete("Test1")
            // .Where("Name = @0")
            // .Parameters("SQLINSERT")
            // .Execute();
            //Console.WriteLine("2.1使用wehre条件进行删除数据:" + _int.ToString() + " " + System.DateTime.Now.ToString());

            ////--Select

            ////1.使用SQL条件查询
            //List<Models.Test> queryResult = _DbHelper.Sql("select * from Test left join Test1 on Test.ID=Test1.TestID where IntID=@IntID").Parameter(new { IntID = 2 }).Query<Models.Test>().ToList();
            //Console.WriteLine("1.1使用wehre条件进行查询数据:" + " " + System.DateTime.Now.ToString());

            ////2.使用SQL条件查询
            //List<Models.Test> queryResult1 = _DbHelper.Select<Models.Test>("Name,IntID").Query().ToList();
            //Console.WriteLine("2.1使用wehre条件进行查询数据:" + queryResult1.Count + "-----" + System.DateTime.Now.ToString());

            ////3.使用where条件
            //List<Models.Test> queryResult2 = _DbHelper.Select<Models.Test>("Name,IntID").Where("IntID=@0").Parameters(2).Query().ToList();
            //Console.WriteLine("3.1使用wehre条件进行查询数据:" + queryResult2.Count + "-----" + System.DateTime.Now.ToString());

            ////4.使用排序
            //List<Models.Test> queryResult3 = _DbHelper.Select<Models.Test>("*").From("Test left join Test1 on Test.ID=Test1.TestID").Where("IntID =@IntID").Parameter(new { IntID = 2 }).OrderBy("Test.ID DESC").Query().ToList();
            //Console.WriteLine("4.1使用wehre条件进行查询数据:" + queryResult3.Count + "-----" + System.DateTime.Now.ToString());

            ////5.使用分页
            //List<Models.Test> queryResult4 = _DbHelper.Select<Models.Test>("*").From("Test left join Test1 on Test.ID=Test1.TestID").Page(1, 2).Where("IntID=2").Query().ToList();
            //Console.WriteLine("5.1使用wehre条件进行查询数据:" + Newtonsoft.Json.JsonConvert.SerializeObject(queryResult4) + "-----" + System.DateTime.Now.ToString());

            ////6.查询结果数
            //var queryResult5 = _DbHelper.Sql("select count(*) from Test").Count();
            //Console.WriteLine("6.1使用wehre条件进行查询数据:" + queryResult5 + "----- " + System.DateTime.Now.ToString());

            ////7.使用SQL查询全部数据
            //List<Models.Test> queryResult11 = _DbHelper.Select<Models.Test>("Name,IntID").Query().ToList();
            //Console.WriteLine("7.1询全部数据:" + queryResult11.Count + "-----" + System.DateTime.Now.ToString());

            ////8.查询全部数据
            //List<Models.Test> queryResult21 = _DbHelper.Select<Models.Test>("Name,IntID").Query().ToList();
            //Console.WriteLine("8.1查询全部数据:" + queryResult21.Count + "-----" + System.DateTime.Now.ToString());

            ////9.使用排序查询全部数据
            //List<Models.Test> queryResult31 = _DbHelper.Select<Models.Test>("*").From("Test left join Test1 on Test.ID=Test1.TestID").OrderBy("Test.ID DESC").Query().ToList();
            //Console.WriteLine("9.1查询全部数据:" + queryResult31.Count + "-----" + System.DateTime.Now.ToString());

            ////10.使用分页查询全部数据
            //List<Models.Test> queryResult41 = _DbHelper.Select<Models.Test>("*").From("Test left join Test1 on Test.ID=Test1.TestID").Page(1, 2).Query().ToList();
            //Console.WriteLine("10.1查询全部数据:" + Newtonsoft.Json.JsonConvert.SerializeObject(queryResult41) + "-----" + System.DateTime.Now.ToString());

            #endregion
            
            Console.ReadKey();
        }
    }
}
